package com.grant.code.controller;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.File;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 幻灯片控制器 - 处理图片幻灯片播放功能
 * 包括幻灯片播放、暂停、缩放等功能
 */
public class SlideShowController {

    @FXML private ImageView slideImageView;
    @FXML private HBox controlBar;
    @FXML private Label statusLabel;

    private Timeline slideTimeline;
    private Stage slideStage;
    private List<File> imageFiles;
    private int currentIndex = 0;
    private double currentScale = 1.0;
    private static final double SCALE_STEP = 0.1;
    
    /**
     * 初始化幻灯片控制器
     */
    public void initialize() {
        setupControls();
    }
    
    /**
     * 设置图片列表
     * @param files 图片文件列表
     * @param startIndex 开始播放的索引
     */
    public void setImageList(List<File> files, int startIndex) {
        this.imageFiles = files;
        this.currentIndex = startIndex >= 0 && startIndex < files.size() ? startIndex : 0;
        updateImage();
    }
    
    /**
     * 开始播放幻灯片
     */
    public void startSlideShow() {
        if (slideTimeline != null) {
            slideTimeline.play();
        } else {
            createTimeline();
            slideTimeline.play();
        }
        updateStatusLabel("播放中");
    }
    
    /**
     * 暂停播放幻灯片
     */
    public void pauseSlideShow() {
        if (slideTimeline != null) {
            slideTimeline.pause();
            updateStatusLabel("暂停");
        }
    }
    
    /**
     * 停止播放幻灯片
     */
    public void stopSlideShow() {
        if (slideTimeline != null) {
            slideTimeline.stop();
            slideTimeline = null;
        }
        
        if (slideStage != null) {
            slideStage.close();
            slideStage = null;
        }
    }
    
    /**
     * 显示下一张图片
     */
    public void nextImage() {
        if (imageFiles == null || imageFiles.isEmpty()) return;
        
        currentIndex = (currentIndex + 1) % imageFiles.size();
        updateImage();
    }
    
    /**
     * 显示上一张图片
     */
    public void previousImage() {
        if (imageFiles == null || imageFiles.isEmpty()) return;
        
        currentIndex = (currentIndex - 1 + imageFiles.size()) % imageFiles.size();
        updateImage();
    }
    
    /**
     * 放大图片
     */
    public void zoomIn() {
        currentScale += SCALE_STEP;
        applyScale();
    }
    
    /**
     * 缩小图片
     */
    public void zoomOut() {
        if (currentScale > SCALE_STEP) {
            currentScale -= SCALE_STEP;
            applyScale();
        }
    }
    
    /**
     * 重置缩放
     */
    public void resetZoom() {
        currentScale = 1.0;
        applyScale();
    }
    
    /**
     * 设置控制栏
     */
    private void setupControls() {
        if (controlBar == null) {
            controlBar = createControlBar();
        }
    }
    
    /**
     * 创建控制栏
     * @return 控制栏组件
     */
    private HBox createControlBar() {
        HBox controlBar = new HBox(10);
        controlBar.setAlignment(Pos.CENTER);
        controlBar.setStyle("-fx-background-color: rgba(0,0,0,0.7); -fx-padding: 10px;");
        
        Button prevBtn = new Button("上一张");
        Button nextBtn = new Button("下一张");
        Button playPauseBtn = new Button("播放");
        Button zoomInBtn = new Button("+");
        Button zoomOutBtn = new Button("-");
        
        prevBtn.setOnAction(e -> previousImage());
        nextBtn.setOnAction(e -> nextImage());
        
        playPauseBtn.setOnAction(e -> {
            if (slideTimeline != null && slideTimeline.getStatus() == Timeline.Status.RUNNING) {
                pauseSlideShow();
                playPauseBtn.setText("播放");
            } else {
                startSlideShow();
                playPauseBtn.setText("暂停");
            }
        });
        
        zoomInBtn.setOnAction(e -> zoomIn());
        zoomOutBtn.setOnAction(e -> zoomOut());
        
        // 创建状态标签
        statusLabel = new Label("就绪");
        statusLabel.setStyle("-fx-text-fill: white;");
        
        controlBar.getChildren().addAll(
            prevBtn, playPauseBtn, nextBtn, zoomInBtn, zoomOutBtn, statusLabel
        );
        
        return controlBar;
    }
    
    /**
     * 创建幻灯片时间线
     */
    private void createTimeline() {
        slideTimeline = new Timeline(new KeyFrame(Duration.seconds(3), e -> nextImage()));
        slideTimeline.setCycleCount(Timeline.INDEFINITE);
    }
    
    /**
     * 更新当前显示的图片
     */
    private void updateImage() {
        if (imageFiles == null || imageFiles.isEmpty() || currentIndex < 0 || currentIndex >= imageFiles.size()) {
            return;
        }
        
        File currentFile = imageFiles.get(currentIndex);
        Image image = new Image(currentFile.toURI().toString(), true);
        
        if (slideImageView != null) {
            slideImageView.setImage(image);
            resetZoom(); // 重置缩放
            updateStatusLabel("显示: " + currentFile.getName() + " (" + (currentIndex + 1) + "/" + imageFiles.size() + ")");
        }
    }
    
    /**
     * 应用缩放比例
     */
    private void applyScale() {
        if (slideImageView != null) {
            slideImageView.setScaleX(currentScale);
            slideImageView.setScaleY(currentScale);
            updateStatusLabel(String.format("缩放: %.1f%%", currentScale * 100));
        }
    }
    
    /**
     * 更新状态标签
     * @param text 状态文本
     */
    private void updateStatusLabel(String text) {
        if (statusLabel != null) {
            statusLabel.setText(text);
        }
    }
    
    /**
     * 独立启动幻灯片播放
     * @param files 图片文件列表
     * @param startIndex 开始索引
     */
    public void startStandaloneSlideShow(List<File> files, int startIndex) {
        // 创建图片查看器
        slideStage = new Stage();
        slideStage.setTitle("幻灯片播放");
        
        // 创建幻灯片视图
        slideImageView = new ImageView();
        slideImageView.setFitWidth(800);
        slideImageView.setFitHeight(600);
        slideImageView.setPreserveRatio(true);
        
        // 创建控制栏
        controlBar = createControlBar();
        
        // 创建根容器
        VBox root = new VBox();
        
        // 创建图片容器
        StackPane imageContainer = new StackPane(slideImageView);
        imageContainer.setStyle("-fx-background-color: black;");
        imageContainer.setPrefSize(800, 600);
        
        root.getChildren().addAll(imageContainer, controlBar);
        
        // 设置场景
        Scene scene = new Scene(root, 800, 600);
        slideStage.setScene(scene);
        
        // 设置图片列表并开始播放
        setImageList(files, startIndex);
        startSlideShow();
        
        // 显示窗口
        slideStage.show();
        
        // 添加关闭窗口处理
        slideStage.setOnCloseRequest(e -> stopSlideShow());
    }
} 